//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "SelectGame.h"

#include "EngineChess.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
__fastcall TfrmSelectGame::TfrmSelectGame(TComponent* Owner)
    : TForm(Owner)
{
}

//---------------------------------------------------------------------------
void __fastcall TfrmSelectGame::btnOkClick(TObject *Sender)
{
    Close();
}

//---------------------------------------------------------------------------
bool __fastcall TfrmSelectGame::Execute()
{
    lbGames->Clear();
    lbGames->Items->AddObject(Chess::STR_GAME_NAME, (TObject*)2);


    lbGames->ItemIndex = -1;

    GameName = "";
    ShowModal();
    if (lbGames->ItemIndex >= 0)
    {
        GameName = lbGames->Items->Strings[lbGames->ItemIndex];
    }

    return GameName.Length();
}

//---------------------------------------------------------------------------
void __fastcall TfrmSelectGame::lbGamesMeasureItem(TWinControl *Control,
      int Index, int &Height)
{
    int th = ((TListBox*)Control)->Canvas->TextHeight("W");
    if (th < ImageList->Height)
        Height = ImageList->Height;
    else
        Height = th;
    Height += 4;
}

//---------------------------------------------------------------------------
void __fastcall TfrmSelectGame::lbGamesDrawItem(TWinControl *Control,
      int Index, TRect &Rect, TOwnerDrawState State)
{
    TCanvas* c = ((TListBox*)Control)->Canvas;
    AnsiString Text = ((TListBox*)Control)->Items->Strings[Index];

    c->FillRect(Rect);
    c->Brush->Style = bsClear;
    c->Pen->Style = psSolid;
    c->Pen->Color = clLtGray;
    c->MoveTo(Rect.left, Rect.Bottom-1);
    c->LineTo(Rect.Right, Rect.Bottom-1);

    Rect.left += 4;
    int cy = Rect.top + (Rect.Height() >> 1);
    ImageList->Draw(c, Rect.left, cy - (ImageList->Height >> 1), (int)((TListBox*)Control)->Items->Objects[Index]);
    Rect.left += ImageList->Width + 4;
    int cx = Rect.left + (Rect.Width() >> 1);
    int twc = c->TextWidth(Text) >> 1;
    int thc = c->TextHeight("W") >> 1;
    c->TextRect(Rect, cx - twc, cy - thc, Text);
    
}

//---------------------------------------------------------------------------

